<template>
    <Modal v-model="show" :title="title">
        <Table ref="table" :columns="tableColumns" :data="tableData" :loading="tableLoading"></Table>
    </Modal>
</template>
<script>

    import NET from '../../controllers/download'
    import DateUtils from '../../../node_modules/iview/src/utils/date'
    import Progress from '../../../node_modules/iview/src/components/progress/progress.vue'
    import Utils from '../../libs/util'
    import Constants from '../../libs/constants'
    import Ajax from '../../libs/ajax';

    export default {
        name: "DownloadTableModal",
        props: {
            value: {
                type: Boolean,
                default: false
            },
            title: {
                type:String,
                default:"特征上传"
            }
        },
        data() {
            return {
                tableColumns: [
                    {
                        title: '上传日期',
                        key: 'download_time',
                        align: 'center',
                        render: (h, {row, column, index}) => {
                            let time = row[column.key]
                            return DateUtils.format(new Date(time), 'yyyy-MM-dd HH:mm:ss')
                        }
                    },
                    {
                        title: '压缩包',
                        key: 'file_name',
                        align: 'center',
                        render: (h, {row, column, index}) => {
                            return row[column.key] || "----"
                        }
                    },
                    {
                        title: '状态',
                        key: 'download_statue',
                        align: 'center',
                        render: (h, {row, column, index}) => {
                            let statue = row[column.key]
                            switch (statue) {
                                case 0:
                                    return "正在解压"
                                    break
                                case 1:
                                    let total = row.download_total
                                    let current = row.download_current
                                    let precent = Math.floor(current * 100 / total)
                                    return (
                                            <div>
                                                <p>正在注册({current}/{total})</p>
                                                <Progress percent={precent} status="active"></Progress>
                                            </div>
                                    )
                                    break
                                case 2:
                                    let path = row.log_path
                                    return h("a",
                                        {
                                            on: {
                                                click: () => {
                                                    Utils.downloadByIframe(`${Ajax.BASE_URL}${Constants.URL_LOGINFO_START_DOWNLOAD}?path=${path}`)
                                                }
                                            }
                                        },
                                        "点击下载处理结果")
                                    break
                                case 5:
                                    return "解压出错"
                                    break
                            }
                        }
                    }
                ],
                tableData: [],
                tableLoading: false,
                show:false
            }
        },
        methods: {
            loadData() {
                this.tableLoading = true
                NET.getDownloadFiles().then(json => {
                    if (json.code == 0) {
                        window.data = json.data
                        this.tableData = json.data
                        this.tableLoading = false
                    }
                })
            },
        },
        watch:{
            value:function(val){
                if(val != this.show){
                    this.show = val
                }
                if(val){
                    this.loadData()
                }
            },
            show:function(val){
                if(val != this.value){
                    this.$emit('input',val)
                }
            }
        }
    }
</script>